import { Node, AudioSource, AudioClip, director } from 'cc';
import Singleton from '../base/Singleton';
import { ResourceMgr } from './ResourceMgr';
import { StoreManager } from './StoreManager';

const MUSIC_STORAGE_KEY = "MUSIC_STORAGE_KEY"

export class AudioManager extends Singleton {

    static get Instance() {
        return super.GetInstance<AudioManager>()
    }

    // 音乐
    private _musicSource: AudioSource
    private _musicSwitch: boolean = true;
    private _musicVolume: number = 1

    // 音效
    private _audioSwitch: boolean = true;
    private _audioSource: AudioSource;
    private _audioVolume: number = 1

    constructor() {
        super()
        // 创建一个节点作为 audioMgr
        let audioMgr = new Node("UIAudioMgr");
        // 添加节点到场景
        // director.getScene().addChild(audioMgr);
        // 标记为常驻节点，这样场景切换的时候就不会被销毁了
        director.addPersistRootNode(audioMgr);

        // 背景音乐
        const musicNode = new Node("UIMusic")
        musicNode.parent = audioMgr;
        this._musicSource = musicNode.addComponent(AudioSource)

        // 音效
        const audioNode = new Node("UIAudio")
        audioNode.parent = audioMgr;
        this._audioSource = audioNode.addComponent(AudioSource);

        // 初始化配置
        this.initConfig()
    }

    /**
     * 初始化
     */
    private initConfig() {
        const storageJson = StoreManager.Instance.getItem(MUSIC_STORAGE_KEY);
        if (storageJson) {
            let storageValue = JSON.parse(storageJson);
            this._musicSwitch = storageValue.musicSwitch;
            this._musicVolume = storageValue.musicVolumn;
            this._audioSwitch = storageValue.audioSwitch;
            this._audioVolume = storageValue.audioVolumn;
        }
    }

    /**
     * 播放音乐
     * @param sound 
     * @returns 
     */
    async playMusic(sound: AudioClip | string) {
        if (!this._musicSwitch) {
            return;
        }
        if (sound instanceof AudioClip) {
            this._musicSource.clip = sound;
            this._musicSource.volume = this._musicVolume;
            this._musicSource.play();
            return;
        }
        const audioClip: AudioClip | null = await ResourceMgr.Instance.loadAudio(sound);
        if (audioClip) {
            this._musicSource.clip = audioClip;
            this._musicSource.volume = this._musicVolume;
            this._musicSource.play();
        }
    }

    /**
     * 切换音乐状态
     * @param musicSwitch 
     */
    switchMusic(musicSwitch: boolean) {
        this._musicSwitch = musicSwitch
        if (musicSwitch) {
            this._musicSource.play()
        } else {
            this._musicSource.stop()
        }
    }

    /**
     * 音乐音量大小
     * @param volumn 
     */
    changeMusicVolumn(volumn: number) {
        this._musicVolume = volumn
    }

    /**
     * 播放音效
     * @param sound 
     * @returns 
     */
    async playAudio(sound: AudioClip | string) {
        if (!this._audioSwitch) {
            return;
        }
        if (sound instanceof AudioClip) {
            this._audioSource.playOneShot(sound, this._audioVolume);
            return;
        }
        const audioClip: AudioClip | null = await ResourceMgr.Instance.loadAudio(sound);
        if (audioClip) {
            this._audioSource.playOneShot(audioClip, this._audioVolume);
        }
    }

    /**
     * 切换音效状态
     * @param audioSwitch 
     */
    switchAudio(audioSwitch: boolean) {
        this._audioSwitch = audioSwitch
    }

    /**
     * 音乐音量大小
     * @param volumn 
     */
    changeAudioVolumn(volumn: number) {
        this._audioVolume = volumn
    }

    /**
     * 停止
     */
    stop() {
        this._musicSource.stop();
        this._audioSource.stop();
    }

    /**
     * 暂停
     */
    pause() {
        this._musicSource.pause();
        this._audioSource.pause();
    }

    /**
     * 重播
     */
    resume() {
        if (this._musicSwitch) {
            this._musicSource.play()
        }
        if (this._audioSwitch) {
            this._audioSource.play();
        }
    }

    save() {
        let storageValue = {
            "musicSwitch": this._musicSwitch,
            "musicVolumn": this._musicVolume,
            "audioSwitch": this._audioSwitch,
            "audioVolumn": this._audioVolume
        }
        StoreManager.Instance.setItem(MUSIC_STORAGE_KEY, JSON.stringify(storageValue))
    }

}

